import server.MasterServer;
import client.Client;


/**
 * @author andi
 *
 */
public class StartFromJar {
	static String[] serverData;
	
	public static void main(String[] args) {
		boolean validArgs;
		if(args[0].equals("client")) {
			validArgs=testArgsClient(args);
			if(validArgs) {
				Client client = new Client(serverData[0], serverData[1]);
				client.start();
			} else {
				startDefaultClient();
			}
		}
		if(args[0].equals("server")) {
			validArgs=testArgsServer(args);
			if(validArgs) {
				MasterServer.main(Integer.parseInt(args[1]));
			} else {
				startDefaultServer();
			}	
		}
	}
	
	
	private static boolean testArgsServer(String[] args) {
		if(args.length==2) {
			if(Integer.parseInt(args[1])>0 && Integer.parseInt(args[1])<=65535) {
				return true;
			}
		}
		return false;
	}

	private static boolean testArgsClient(String[] args) {
		if(args.length==2) {
			serverData = args[1].split(":");
			if(serverData.length==2) {
				return true;
			}
		}
		return false;
		
	}
	
	private static void startDefaultClient() {
		Client client = new Client("localhost", "11122");
		client.start();
	}
	
	private static void startDefaultServer() {
		MasterServer.main(11122);
	}
}
